package com.sp2p.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

import com.shove.data.DataException;
import com.shove.data.DataSet;
import com.shove.data.dao.MySQL;
import com.shove.util.BeanMapUtils;
import com.sp2p.database.Dao;

public class GofullAwardDao {
	/**
	 * 添加共富奖励
	 * @param conn
	 * @param userId
	 * @param investor
	 * @param awardMoney
	 * @param status
	 * @return
	 * @throws SQLException
	 */
	public Long addGofullAward(Connection conn,long userId,long investor,BigDecimal awardMoney,int status,BigDecimal amountDouble,long borrowId) throws SQLException{
		Dao.Tables.t_gofull_award t_gofull_award = new Dao().new Tables().new t_gofull_award();
		t_gofull_award.userId.setValue(userId);
		t_gofull_award.investor.setValue(investor);
		t_gofull_award.awardMoney.setValue(awardMoney);
		t_gofull_award.investTime.setValue(new Date());
		t_gofull_award.status.setValue(status);
		t_gofull_award.investAmount.setValue(amountDouble);
		t_gofull_award.borrowId.setValue(borrowId);
		
		return t_gofull_award.insert(conn);
	}
	/**
	 * 查询共富奖励
	 * @param conn
	 * @param userId
	 * @param investor
	 * @param awardMoney
	 * @param status
	 * @return
	 * @throws SQLException
	 * @throws DataException 
	 */
	public Map<String,String> queryGofullAwardone(Connection conn,String id) throws SQLException, DataException{
		Dao.Tables.t_gofull_award t_gofull_award = new Dao().new Tables().new t_gofull_award();
		String commom = "";
//		if(year != 0){
//			commom += " and YEAR(investTime)= " +year;
//		}
//		if(month != 0){
//			commom += " and MONTH(investTime )= " +month;
//		}
//		
		DataSet dataSet =  t_gofull_award.open(conn, "*", "id = " + id + commom, "", 0, 1);
		return BeanMapUtils.dataSetToMap(dataSet);
	}
	/**
	 * 查询共富奖励
	 * @param conn
	 * @param userId
	 * @param investor
	 * @param awardMoney
	 * @param status
	 * @return
	 * @throws SQLException
	 * @throws DataException 
	 */
	public Map<String,String> queryGofullAward(Connection conn,String id) throws SQLException, DataException{
		Dao.Tables.t_gofull_award t_gofull_award = new Dao().new Tables().new t_gofull_award();
		
		DataSet dataSet =  t_gofull_award.open(conn, "*", "id = " + id, "", 0, 1);
		return BeanMapUtils.dataSetToMap(dataSet);
	}
	/**
	 * 添加共富奖励
	 * @param conn
	 * @param userId
	 * @param investor
	 * @param awardMoney
	 * @param status
	 * @return
	 * @throws SQLException
	 * @throws DataException 
	 */
	public Long updateGofullAward(Connection conn,String id) throws SQLException, DataException{
		Dao.Tables.t_gofull_award t_gofull_award = new Dao().new Tables().new t_gofull_award();
		t_gofull_award.status.setValue("2");
		Long i  =  t_gofull_award.update(conn, "id = "+id);
		return i;
	}
	/**
	 * 统计共富奖励总额
	 * 
	 * @throws DataException
	 * @throws SQLException
	 */
	public Map<String, String> queryTotalGofullAward(Connection conn)
			throws SQLException, DataException {
		StringBuffer sql = new StringBuffer();
		sql.append("select sum(awardMoney) totalAwardMoney from t_gofull_award");
		DataSet dataSet = MySQL.executeQuery(conn, sql.toString());
		sql= null;
		return BeanMapUtils.dataSetToMap(dataSet);
	}
}
